// pages/homeTab/mate/mate.js
const app = getApp();
var util = require('../../../utils/util.js')
var dur = 1;
const date = new Date();
const hours = [];
const thours = [];
const minutes = ['00', '30'];
//获取小时
for (let i = 0; i < 24; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  hours.push("" + i);
};
for (let i = 0; i < 24; i++) {
  if (i == 0) {
    i = "立即开始";
    thours.push(i);
    i = 0;
    i = "0" + i;
    thours.push("" + i);
    continue;
  } else if (i > 0 && i < 10) {
    i = "0" + i;
  }
  thours.push("" + i);
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    faceId: '',
    head: '',
    btn: '',
    btn_disabled: false,
    game: ['王者荣耀'],
    objectGame: [
      {
        id: 0,
        name: '王者荣耀'
      }
    ],
    system: ['安卓', 'IOS'],
    objectSystem: [
      {
        id: 0,
        name: '安卓',
        id: 1,
        name: 'IOS'
      }
    ],
    platform: ['QQ', '微信'],
    objectPlatform: [
      {
        id: 0,
        name: 'QQ',
        id: 1,
        name: '微信'
      }
    ],
    rank: ['秩序白银', '荣耀黄金', '尊贵铂金', '永恒钻石V', '永恒钻石IV-III', '永恒钻石II-I', '至尊星耀V-III', '至尊星耀II-I', '王者1-10', '王者11-20', '王者21-25', '王者26-30', '王者31-35', '王者36-40', '王者41-45', '王者46-50', '王者51-100'],
    objectRank: [
      {
        id: 0,
        name: '秩序白银',
        id: 1,
        name: '荣耀黄金',
        id: 2,
        name: '尊贵铂金',
        id: 3,
        name: '永恒钻石V',
        id: 4,
        name: '永恒钻石Ⅳ-Ⅲ',
        id: 5,
        name: '永恒钻石Ⅱ-Ⅰ',
        id: 6,
        name: '至尊星耀V-Ⅲ',
        id: 7,
        name: '至尊星耀Ⅱ-Ⅰ',
        id: 8,
        name: '最强王者1-10',
        id: 9,
        name: '最强王者11-20',
        id: 10,
        name: '最强王者21-25',
        id: 11,
        name: '最强王者26-30',
        id: 12,
        name: '最强王者31-35',
        id: 13,
        name: '最强王者36-40',
        id: 14,
        name: '最强王者41-45',
        id: 15,
        name: '最强王者46-50',
        id: 16,
        name: '最强王者51-100'
      }
    ],
    time: '02:00',
    timeArray: [thours, minutes],
    timeIndex: [3, 0],
    stime: '02:00',
    stimeArray: [thours, minutes],
    stimeIndex: [3, 0],
    etime: '03:00',
    duration: ['1小时', '2小时', '3小时', '4小时'],
    objectDuration: [
      {
        id: '0',
        name: '1小时',
        id: '1',
        name: '2小时',
        id: '2',
        name: '3小时',
        id: '3',
        name: '4小时',
      }
    ],
    sclass: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
    index1: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    index5: 0,
    index6: 0,
    shour: '02',
    sminute: '00',
    avatarUrl: '',
    nickName: [],
    score: '',
    preTime: '',
    courseInfo: '',
    courseType: '',
    coursePr: '',
    level: '',
    teacherId: '',
  },

  bindGameChange: function (e) {
    this.setData({
      index1: e.detail.value
    })
  },

  bindSystemChange: function (e) {
    this.setData({
      index2: e.detail.value
    })
  },

  bindPlatformChange: function (e) {
    this.setData({
      index3: e.detail.value
    })
  },

  bindRankChange: function (e) {
    this.setData({
      index4: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    this.setData({
      timeIndex: e.detail.value
    })
    const index = this.data.timeIndex;
    if (index[0] == 0) {
      var ttime = util.formatDate(new Date());
      this.setData({
        time: ttime
      })
    } else {
      const hour = this.data.timeArray[0][index[0]];
      const minute = this.data.timeArray[1][index[1]];
      this.setData({
        time: hour + ':' + minute
      })
    }
  },
  bindTimeColumnChange: function (e) {
    var data = {
      timeArray: this.data.timeArray,
      timeIndex: this.data.timeIndex
    };
    data.timeIndex[e.detail.column] = e.detail.value;
    this.setData(data);
  },

  bindStimeChange: function (e) {
    this.setData({
      stimeIndex: e.detail.value
    })
    const index = this.data.stimeIndex;
    if (index[0] == 0) {
      var ttime = util.formatDate(new Date());
      this.setData({
        stime: ttime
      })
      const shour = util.formatHour(new Date());
      const minute = util.formatMinute(new Date());
      this.setData({
        shour: shour,
        sminute: minute
      })
      this.bindEtimeChange();
    } else {
      const hour = this.data.stimeArray[0][index[0]];
      const minute = this.data.stimeArray[1][index[1]];
      this.setData({
        stime: hour + ':' + minute,
        shour: hour,
        sminute: minute
      })
      this.bindEtimeChange();
    }
  },
  bindStimeColumnChange: function (e) {
    var data = {
      stimeArray: this.data.stimeArray,
      stimeIndex: this.data.stimeIndex
    };
    data.stimeIndex[e.detail.column] = e.detail.value;
    this.setData(data);
  },

  bindEtimeChange: function () {
    const index = this.data.stimeIndex;
    if (index[0] == 0) {
      var shour = Number(this.data.shour);
      var ehour = dur + shour;
      var eminute = this.data.sminute;
      this.setData({
        etime: ehour + ':' + eminute
      })
    } else {
      var shour = Number(this.data.shour);
      var ehour = dur + shour;
      var eminute = this.data.sminute;
      if (ehour < 10) {
        ehour = '0' + ehour;
      } else if (ehour >= 24) {
        ehour = ehour - 24;
        ehour = '0' + ehour;
      }
      this.setData({
        etime: ehour + ':' + eminute
      })
    }
  },

  bindDurationChange: function (e) {
    this.setData({
      index6: e.detail.value
    })
    var index = Number(this.data.index6);
    dur = index + 1;
    //time = time.toFixed(1);
    this.bindEtimeChange();
  },

  bindSclassChange: function (e) {
    this.setData({
      index5: e.detail.value
    })
    var index = Number(this.data.index5);
    dur = index + 1;
  },

  bindMate: function () {
    this.setData({
      btn_mate: submit
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      faceId: options.id,
    })
    const faceId = options.id;
    if (faceId == 0) {
      that.setData({
        head: '上分冲刺',
        timeStatus: true,
        stimeStatus: false,
        etimeStatus: false,
        durationStatus: false,
        sclassStatus: true,
        btn: '开始冲刺',
      })
    } else if (faceId == 10) {
      that.setData({
        head: '入门教学课',
        timeStatus: false,
        stimeStatus: true,
        etimeStatus: true,
        durationStatus: true,
        sclassStatus: false,
        btn: '开始上课',
      })
    } else if (faceId == 11) {
      that.setData({
        head: '实战教学课',
        timeStatus: false,
        stimeStatus: true,
        etimeStatus: true,
        durationStatus: true,
        sclassStatus: false,
        btn: '开始上课',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  submit: function () {
    if (dur <= 0) {
      wx.showToast({
        title: '时间错误\r\n请重新选择',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    var that = this;
    const faceId = this.data.faceId;
    const index1 = this.data.index1;
    const index2 = this.data.index2;
    const index3 = this.data.index3;
    const index4 = Number(this.data.index4);
    var game = this.data.game[index1];
    var platform = this.data.platform[index3];
    var rank = index4*1 + 1;
    var system = index2*1+1;
    var stime = that.data.stime;
    var etime = that.data.etime;
    var duration = dur;
    if (faceId == 0) {
      var courseType = 3;
    } else if (faceId == 10) {
      var courseType = 1;
    } else if (faceId == 11) {
      var courseType = 2;
    }
    app.globalData.platform = platform;
    app.globalData.gameChoice = game;
    app.globalData.levelCode = this.data.rank[rank*1-1];
    app.globalData.system = system;
    app.globalData.courseType = courseType;
    app.globalData.shichang1 = dur;
    app.globalData.shichang = stime+"-"+etime;
    app.globalData.levelCode_code = rank;
    console.log(system,'system')
    wx.request({
      url: 'https://www.enjoyyz.com:8443/yinzuo/match',
      method: 'GET',
      data: {
        platform: platform,
        gameChoice: game,
        levelCode: rank,
        system: system,
        courseType: courseType,
        duration: duration,
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        const data_num = res.data.length;
        console.log(res.data);
        if (data_num > 0) {
          var num_rand = 0;
          var max = data_num - 1;
          num_rand = Math.round(Math.random() * max);
          this.setData({
            avatarUrl: res.data[num_rand].pic,
            nickName: res.data[num_rand].userName,
            score: res.data[num_rand].evaluateScore,
            preTime: res.data[num_rand].onlineTime,
            courseInfo: res.data[num_rand].courseInfo,
            courseType: res.data[num_rand].courseType,
            coursePr: res.data[num_rand].price,
            level: res.data[num_rand].level,
            teacherId: res.data[num_rand].teacherId,
          })
          app.globalData.searchResult = res.data[num_rand];
          var that = this;
          wx.navigateTo({
            url: '../success/success?avatarUrl=' + that.data.avatarUrl + '&nickName=' + that.data.nickName + '&score=' + that.data.score + '&preTime=' + that.data.preTime + '&courseInfo=' + that.data.courseInfo + '&courseType=' + that.data.courseType + '&coursePr=' + that.data.coursePr + '&level=' + that.data.level + '&teacherId=' + that.data.teacherId,
          })
        } else if (data_num == 0 || data_num < 0) {
          wx.showToast({
            title: '无匹配对象，请修改条件后再试！',
            icon: 'none',
            duration: 2500,
          })
        }
      },
      fail() {
        wx.showToast({
          title: '匹配错误，请稍后再试！',
          icon: 'none',
          duration: 2500,
        })
      }
    });
  },
})